/*
 * @Author: szx
 * @Date: 2022-04-13 11:45:04
 * @LastEditTime: 2022-04-13 12:03:36
 * @Description:
 * @FilePath: \leetcode\剑指offerII\058\058.js
 */
var MyCalendar = function () {
    this.arr = [];
};

/**
 * @param {number} start
 * @param {number} end
 * @return {boolean}
 */
MyCalendar.prototype.book = function (start, end) {
    for (let [s, e] of this.arr) {
        if (!(start >= e || end <= s)) return false;
    }
    this.arr.push([start, end]);
    return true;
};

var MyCalendar = function () {
    this.arr = [];
};
MyCalendar.prototype.findInsertIndex = function (start) {
    let left = 0;
    let right = this.arr.length - 1;
    while (left <= right) {
        let mid = Math.floor(left + (right - left) / 2);
        if (this.arr[mid][0] === start) {
            return mid;
        }
        if (this.arr[mid][0] < start) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return left;
};

MyCalendar.prototype.book = function (start, end) {
    let t = this.findInsertIndex(start);
    if ((this.arr[t - 1] && start < this.arr[t - 1][1]) || (this.arr[t] && end > this.arr[t][0])) {
        return false;
    }
    this.arr.splice(t, 0, [start, end]);
    return true;
};

/**
 * Your MyCalendar object will be instantiated and called as such:
 * var obj = new MyCalendar()
 * var param_1 = obj.book(start,end)
 */
